TryAdd Method (T, Int32, CancellationToken)

Task Parallel System.Threading

Attempts to add the specified item to the BlockingCollection<(Of <(T>)>). A OperationCanceledException is thrown if the CancellationToken is canceled.

Namespace:  System.Collections.Concurrent
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
Public Function TryAdd ( _
	item As T, _
	millisecondsTimeout As Integer, _
	cancellationToken As CancellationToken _
) As Boolean
C#
public bool TryAdd(
	T item,
	int millisecondsTimeout,
	CancellationToken cancellationToken
)

Parameters

item
Type: T
The item to be added to the collection.
millisecondsTimeout
Type: System..::.Int32
The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
cancellationToken
Type: System.Threading..::.CancellationToken
A cancellation token to observe.

Return Value

true if the item could be added to the collection within the alloted time; otherwise, false.

Exceptions

ExceptionCondition
System..::.OperationCanceledExceptionIf the CancellationToken is canceled.
System..::.InvalidOperationExceptionThe BlockingCollection<(Of <(T>)>) has been marked as complete with regards to additions.
System..::.ObjectDisposedExceptionThe BlockingCollection<(Of <(T>)>) has been disposed.
System..::.ArgumentOutOfRangeExceptionmillisecondsTimeout is a negative number other than -1, which represents an infinite time-out.
System..::.InvalidOperationExceptionThe underlying collection didn't accept the item.

See Also